package com.animee.tally;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.animee.tally.adapter.AccountAdapter;
import com.animee.tally.db.AccountBean;
import com.animee.tally.db.DBManager;

import java.util.ArrayList;
import java.util.List;
public class SearchActivity extends AppCompatActivity {
    ListView searchLv;
    EditText searchEt;
    TextView searchTv;
    List<AccountBean>mDatas;   //listview显示的数据源
    AccountAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        searchEt = findViewById(R.id.search_et);
        searchLv = findViewById(R.id.search_lv);
        searchTv = findViewById(R.id.search_tv);
        mDatas = new ArrayList<>();   //初始化集合
        //设置适配器
        adapter = new AccountAdapter(this, mDatas);
        searchLv.setAdapter(adapter);
        searchLv.setEmptyView(searchTv);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_iv_back:
                finish();
                break;
            case R.id.search_iv_sh:   //开始执行搜索操作
                String text = searchEt.getText().toString();
//                判断输入内容是否为空，如果为空，提示不能搜索
                if (TextUtils.isEmpty(text)) {
                    Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                //开始搜索
                List<AccountBean> list = DBManager.searchInfoByAccount(text);
                mDatas.clear();
                mDatas.addAll(list);
                adapter.notifyDataSetChanged();
                break;
        }
    }
}
